package com.jinzhi.job.quartz;

import com.jinzhi.job.domain.ScheduleJobDO;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.DateBuilder.IntervalUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

;


/**
 * <pre>
 * 定时任务管理
 * </pre>
 * <small> 2018年3月23日 | Aron</small>
 */
@Service
@Slf4j
public class QuartzManager {
    @Autowired
    private Scheduler scheduler;

    /**
     * 添加任务
     */
    @SuppressWarnings("unchecked")
    public void addJob(ScheduleJobDO job) {
        try {
            // 创建jobDetail实例，绑定Job实现类
            Class<? extends Job> jobClass = (Class<? extends Job>) (Class.forName(job.getBeanClass()).newInstance()
                    .getClass());
            // 任务名称和组构成任务key
            JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(new JobKey(job.getJobName(), job.getJobGroup()))
                    .build();
            // 定义corn表达式调度触发器
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(new TriggerKey(job.getJobName(), job.getJobGroup()))
                    .startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
                    .withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpression())).startNow().build();
            // 把作业和触发器注册到任务调度中
            scheduler.scheduleJob(jobDetail, trigger);
            // 启动
            if (!scheduler.isShutdown()) {
                scheduler.start();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 获取所有计划中的任务列表
     */
    public List<ScheduleJobDO> getAllJob() throws SchedulerException {
//        GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
//        Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
//        List<ScheduleJobDO> jobList = new ArrayList<>();
//        for (JobKey jobKey : jobKeys) {
//            List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
//            for (Trigger trigger : triggers) {
//                ScheduleJobDO job = new ScheduleJobDO();
//                job.setJobName(jobKey.getName());
//                job.setJobGroup(jobKey.getGroup());
//                job.setDescription("触发器:" + trigger.getKey());
//                Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
//                job.setJobStatus(triggerState.name());
//                if (trigger instanceof CronTrigger) {
//                    CronTrigger cronTrigger = (CronTrigger) trigger;
//                    String cronExpression = cronTrigger.getCronExpression();
//                    job.setCronExpression(cronExpression);
//                }
//                jobList.add(job);
//            }
//        }
        return null;
    }

    /**
     * 所有正在运行的job
     */
    public List<ScheduleJobDO> getRunningJob() throws SchedulerException {
//        List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
//        List<ScheduleJobDO> jobList = new ArrayList<ScheduleJobDO>(executingJobs.size());
//        for (JobExecutionContext executingJob : executingJobs) {
//            ScheduleJobDO job = new ScheduleJobDO();
//            JobDetail jobDetail = executingJob.getJobDetail();
//            JobKey jobKey = jobDetail.getKey();
//            Trigger trigger = executingJob.getTrigger();
//            job.setJobName(jobKey.getName());
//            job.setJobGroup(jobKey.getGroup());
//            job.setDescription("触发器:" + trigger.getKey());
//            Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
//            job.setJobStatus(triggerState.name());
//            if (trigger instanceof CronTrigger) {
//                CronTrigger cronTrigger = (CronTrigger) trigger;
//                String cronExpression = cronTrigger.getCronExpression();
//                job.setCronExpression(cronExpression);
//            }
//            jobList.add(job);
//        }
        return null;
    }

    /**
     * 暂停一个job
     */
    public void pauseJob(ScheduleJobDO scheduleJob) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
        scheduler.pauseJob(jobKey);
    }

    /**
     * 恢复一个job
     */
    public void resumeJob(ScheduleJobDO scheduleJob) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
        scheduler.resumeJob(jobKey);
    }

    /**
     * 删除一个job
     */
    public void deleteJob(ScheduleJobDO scheduleJob) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
        scheduler.deleteJob(jobKey);

    }

    /**
     * 立即执行job
     */
    public void runAJobNow(ScheduleJobDO scheduleJob) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
        scheduler.triggerJob(jobKey);
    }

    /**
     * 更新job时间表达式
     */
    public void updateJobCron(ScheduleJobDO scheduleJob) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
        scheduler.rescheduleJob(triggerKey, trigger);
    }
}